<?php

class DevPage
{
	private $total;
	private $page;
	private $rowPerPage;
	private $startId;
	private $pagenav;
	private $url;
	private $lastPage;
	private $prePage;
	private $nextPage;
	public function __construct($total,$rowPerPage)
	{
		$this->total = $total;

		$this->page = isset($_GET['page']) ? $_GET['page'] : 1;
		if(!$this->page) $this->page = 1;
		$this->url = $_SERVER['REQUEST_URI'];
		$parse_url = parse_url($this->url);
		$url_query='';
		if(isset($parse_url["query"]))$url_query = $parse_url["query"]; //get query string
		if ($url_query) {
			$url_query = ereg_replace("(^|&)page=$this->page", "", $url_query);
			$this->url = str_replace($parse_url["query"], $url_query, $this->url);
			if ($url_query)
				$this->url .= "&page";
			else
				$this->url .= "page";
		} else {
			$this->url .= "?page";
		}

		$this->rowPerPage = $rowPerPage;
		$this->lastPage = ceil($total / $rowPerPage); //last page and also the total page
		$this->page = min($this->lastPage, $this->page);
		$this->prePage = $this->page -1; //pre page
		$this->nextPage = ($this->page == $this->lastPage ? 0 : $this->page +1); //next page
		$this->startId = ($this->page-1) * $rowPerPage;
		$this->startId = ($this->startId>0) ? $this->startId : 0;
	}

	public function GetLastPage()
	{
		return $this->lastPage;
	}

	public function GetPrePage()
	{
		return $this->prePage;
	}

	public function GetNextPage()
	{
		return $this->nextPage;
	}

	public function GetRowPerPage()
	{
		return $this->rowPerPage;
	}

	public function GetStartId()
	{
		return $this->startId;
	}

	public function GetPageNav()
	{
		//$pagenav = "显示第 <B>" . ($total ? ($firstcount +1) : 0) . "</B>-<B>" . min($firstcount + $displaypg, $total) . "</B> 条记录，共 $total 条记录";
		$pagenav= "共 ".$this->total." 条记录";
		if ($this->lastPage <= 1) return false;

		$pagenav .= " <a href='".$this->url."=1'>首页</a> ";
		if ($this->prePage)
			$pagenav .= " <a href='".$this->url."=".$this->prePage."'>上一页</a> ";
		else
			$pagenav .="上一页";

		if ($this->nextPage)
			$pagenav .= " <a href='".$this->url."=".$this->nextPage."'>下一页</a> ";
		else
			$pagenav .="下一页";
		$pagenav .= " <a href='".$this->url."=".$this->lastPage."'>尾页</a> ";

		//[ch]下拉跳转列表，循环列出所有页码：
		$pagenav .= "　到第 <select name='topage' size='1' onchange='window.location=\"$this->url=\"+this.value'>\n";
		for ($i = 1; $i <= $this->lastPage; $i++) {
			if ($i == $this->page)
				$pagenav .= "<option value='$i' selected>$i</option>\n";
			else
				$pagenav .= "<option value='$i'>$i</option>\n";
		}
		$pagenav .= "</select> 页，共 ".$this->lastPage." 页";
		return $pagenav;
	}
}
?>